package my.layer.tool;

import my.layer.core.LayerAble;
import my.myspring.tool.SpringUtil;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2010-10-14
 * Time: 12:52:35
 * To change this template use File | Settings | File Templates.
 */
public class LayerHandler implements LayerAble {
    private static LayerHandler layerHandler = new LayerHandler();
    private static List<LayerAble> layers = (List<LayerAble>) SpringUtil.getBean("LayerList");

    private LayerHandler() {
    }

    public static LayerHandler getInstance() {
        return layerHandler;
    }

    public String encode(String load) throws Exception {
        if (layers == null) return load;
        for (int i = 0; i < layers.size(); i++) {
            load = layers.get(i).encode(load);
        }
        return load;
    }

    public String decode(String load) throws Exception {
        if (layers == null) return load;
        for (int i = layers.size()-1; i>=0 ; i--) {
            load = layers.get(i).decode(load);
        }
        return load;
    }
}
